package cn.gz.demo.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.RatingBar;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import cn.gz.demo.R;
import cn.gz.demo.databinding.ScoreBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.LogUtil;
import cn.gz.demo.utils.ToastUtil;

/**
 * @author Liuyz
 * @date 2020/12/1 18:04
 */
public class ScoreActivity extends BaseActivity {
    private ScoreBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_score);

        binding.rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                ToastUtil.showToast("" + rating);
            }
        });

        int color = getViewColor(255, 0, 0, 127);
        LogUtil.showLog(".......color............" + color);

        int color1 = changeAlpha(ContextCompat.getColor(context, R.color.red), 0.5f);
        LogUtil.showLog(".......color1............" + color1);
        binding.tv.setTextColor(0xffff00ff);
    }

    private int getViewColor(int red, int green, int blue, int alpha) {

        if (red > 255 || red < 0) {
            red = 255;
        }
        if (green > 255 || green < 0) {
            green = 255;
        }
        if (blue > 255 || blue < 0) {
            blue = 255;
        }
        if (alpha > 255 || alpha < 0) {
            alpha = 255;
        }
        return Color.argb(alpha, red, green, blue);

    }

    public static int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);

        LogUtil.showLog("....red：" + red+"....green：" + green+"....blue：" + blue+"....alpha：" + alpha);
        return Color.argb(alpha, red, green, blue);
    }
}
